package com.main;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.dom.DOMDocument;
import org.dom4j.io.SAXReader;

import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * xml转换工具
 * Created by 瑱彬 on 2016/8/4.
 */
public class XmlParseUtil {
    public static Map<String,String> toMap(String xmlStr){
        try {
            Map<String,String> resultMap = new HashMap<>();
            Document document = DocumentHelper.parseText(xmlStr);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            for ( Element e : elements) {
                String name = e.getName();
                Object data = e.getData();
                resultMap.put(name,data.toString());
            }
            return resultMap;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(toMap("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx00e324152827a7a8]]></appid><mch_id><![CDATA[1365264302]]></mch_id><nonce_str><![CDATA[WHUqWHmcVH5nJ7q6]]></nonce_str><sign><![CDATA[81C33553C3507E6838374C2F75DCD249]]></sign><result_code><![CDATA[SUCCESS]]></result_code><prepay_id><![CDATA[wx201608041603354567cf22750668720778]]></prepay_id><trade_type><![CDATA[APP]]></trade_type></xml>"));
    }
}
